package com.example.neteasecloudmusic.ui.dicovery.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.lifecycle.LifecycleOwner;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.neteasecloudmusic.R;
import com.example.neteasecloudmusic.bean.discoveryBean.Banners;
import com.example.neteasecloudmusic.bean.discoveryBean.Blocks;
import com.example.neteasecloudmusic.bean.discoveryBean.Creatives;
import com.example.neteasecloudmusic.view.LinearSpaceItemDecoration;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;

import java.util.ArrayList;
import java.util.List;

public class DiscoveryAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {

    private RecyclerView recycleView;
    private LayoutInflater layoutInflater;
    private Context context;
    private List<Blocks> data;
    private OnItemClickListener listener;

    public DiscoveryAdapter(RecyclerView recyclerView, Context context, List<Blocks> blocks) {
        this.recycleView = recyclerView;
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        this.data = blocks;
    }

    public void setData (List<Blocks> blocks) {
        this.data = blocks;
        notifyDataSetChanged();
    }

    /**
     * for create ViewHolder instance
     * @param parent
     * @param viewType
     * @return
     */
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // layoutInflater.inflate convert xml resource to View object
        View view = layoutInflater.inflate(viewType, parent, false);
//        view.setOnClickListener(this);

        if (viewType == R.layout.discovery_recycle_text_image) {
            return new MultiViewHolder(view);
        } else if (viewType == R.layout.discovery_recycle_option) {
            return new OptionViewHolder(view);
        }
        return new SingleViewHolder(view);
    }

    /**
     * for assign value in subject item of RecycleView
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        Blocks blocks = data.get(position);
        int itemViewType = getItemViewType(position);
        switch (itemViewType) {
            case R.layout.discovery_recycle_banner:
                {
                    List<String> url = new ArrayList<>();
                    List<Banners> banners = blocks.getExtInfo().getBanners();
                    for (Banners banner : banners) {
                        url.add(banner.getPic());
                    }
                    ((Banner) holder.itemView).setAdapter(new BannerImageAdapter<String>(url) {
                        @Override
                        public void onBindView(BannerImageHolder holder, String data, int position, int size) {
                            //图片加载自己实现
                            Glide.with(holder.itemView)
                                    .load(data)
                                    .apply(RequestOptions.centerCropTransform())
                                    .into(holder.imageView);
                        }
                    })
                            .addBannerLifecycleObserver((LifecycleOwner) context)//添加生命周期观察者
                            .setIndicator(new CircleIndicator(context));
                }
                break;
            case R.layout.discovery_recycle_text_image: {
                List<Creatives> creatives = blocks.getCreatives();
                MultiViewHolder multiViewHolder = (MultiViewHolder) holder;
                multiViewHolder.textView.setText(blocks.getUiElement().getSubTitle().getTitle());
                SongMenuAdapter songMenuAdapter = new SongMenuAdapter(context, creatives);
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context, RecyclerView.HORIZONTAL, false);
                multiViewHolder.menuRv.addItemDecoration(new LinearSpaceItemDecoration(context.getResources().getDimensionPixelSize(R.dimen.albumMarginSize)));
                multiViewHolder.menuRv.setLayoutManager(linearLayoutManager);
                multiViewHolder.menuRv.setAdapter(songMenuAdapter);
                multiViewHolder.menuRv.setVisibility(View.VISIBLE);

            } break;
            case R.layout.discovery_recycle_option: {

            } break;

        }
    }

    @Override
    public int getItemViewType(int position) {
        Blocks blocks = data.get(position);
        if (blocks.getShowType().equals("BANNER")) {
            return R.layout.discovery_recycle_banner;
        } else if (blocks.getShowType().equals("HOMEPAGE_OPTION")) {
            return R.layout.discovery_recycle_option;
        } else {
            return R.layout.discovery_recycle_text_image;
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    @Override
    public void onClick(View v) {
        if (listener != null) {
            int position = recycleView.getChildAdapterPosition(v);
            listener.onItemClick(String.valueOf(position));
        }
    }

    class SingleViewHolder extends RecyclerView.ViewHolder {

        public SingleViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }


    class MultiViewHolder extends RecyclerView.ViewHolder {

        TextView textView;
        RecyclerView menuRv;

        public MultiViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.text);
            menuRv = itemView.findViewById(R.id.songMenuRv);
        }
    }

    class OptionViewHolder extends RecyclerView.ViewHolder {

        TextView textView;

        public OptionViewHolder(View view) {
            super(view);
        }
    }

    public void setOnItemClickListener (OnItemClickListener listener) {

        this.listener = listener;

    }

    public interface OnItemClickListener {
        void onItemClick(String id);
    }


}


























